﻿using Microsoft.EntityFrameworkCore;
using XsqTech.SmartOA.Domain.Table;
using XsqTech.SmartOA.Dtos.DTO.Dict;
using XsqTech.SmartOA.EFCore;
using XsqTech.SmartOA.EFCore.Respository;
using XsqTech.SmartOA.IRespository;
using XsqTech.SmartOA.IService;

namespace XsqTech.SmartOA.Service
{
    public class DictService: IDictService
    {
        IDictRespository _basedict;
        readonly IUnitOfWork _unitOfWork;
        public MyDbContext _db;

        public DictService(IDictRespository basedict, IUnitOfWork unitOfWork, MyDbContext db)
        {
            _basedict = basedict;
            _unitOfWork = unitOfWork;
            _db = db;
        }
        /// <summary>
        /// 数据字典树形主表
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public List<Dict> GetDict()
        {
            ReturnDTO<Dict> dTO = new ReturnDTO<Dict>();
            var list = _basedict.Query().Where(x => x.DictFId == 0).ToList();
            return list;
        }
        /// <summary>
        /// 数据字典主表添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnDTO<Dict> PustDictAdd(Dict model)
        {
            ReturnDTO<Dict> dTO = new ReturnDTO<Dict>();
            try
            {
                model.DictCreateTime = DateTime.Now;
                model.DictUpdateTime = DateTime.Now;
                model.DictState = 1;
                model.DictFId = null;
                _db.Database.ExecuteSqlRaw($"insert into dicts(DictName,DictCreateTime,DictCreateUser,DictUpdateTime,DictUpdateUser,DictState,DictCode,DictFId) values('{model.DictName}','{model.DictCreateTime}','{model.DictCreateUser}','{model.DictUpdateTime}','{model.DictUpdateUser}',{model.DictState},'{model.DictCode}',{model.DictFId})");
                var i = _unitOfWork.SaveChangs();
                dTO.Mag = "操作成功";
                dTO.State = true;
                return dTO;
            }
            catch (Exception ex)
            {

                dTO.Mag = ex.Message;
                dTO.State = false;
                return dTO;
            }


        }
        /// <summary>
        /// 数据字典修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateDict(DictUpdateDto model)
        {
            model.DictUpdateTime = DateTime.Now;
            var rr = $"update dicts set DictName='{model.DictName}',DictUpdateUser='{model.DictUpdateUser}',DictCode='{model.DictCode}',DictState={model.DictState}where DictId={model.Dictid}";
            _db.Database.ExecuteSqlRaw(rr);
            var ll = _db.SaveChanges();
            return ll;
        }
        /// <summary>
        /// 数据字典删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteDict(int id)
        {
            _basedict.Delete(id);
            return _unitOfWork.SaveChangs();
        }
        /// <summary>
        /// 根据主键获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Dict DictMainRefill(int id)
        {
            return _basedict.GetBysId(id);
        }
    }
}